package cn.xm.pet.controller;

import cn.xm.basic.dto.AcceptDto;
import cn.xm.basic.util.LoginContext;
import cn.xm.org.domain.Employee;
import cn.xm.org.mapper.EmployeeMapper;
import cn.xm.pet.domain.SearchMasterMsg;
import cn.xm.pet.query.SearchMasterMsgQuery;
import cn.xm.basic.util.AjaxResult ;
import cn.xm.basic.util.PageList;
import cn.xm.pet.service.SearchMasterMsgService;
import cn.xm.user.domain.Logininfo;
import cn.xm.user.domain.User;
import cn.xm.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public SearchMasterMsgService searchMasterMsgService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.add(searchMasterMsg);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("保存对象失败" + e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.queryByPage(query);
    }

    /**
     * 寻主消息发布
     * @param searchMasterMsg
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg,HttpServletRequest request)
    {
        // 查找到userid
        Logininfo logininfo = LoginContext.getLogininfo(request);
        User user = userMapper.findByLogininfoId(logininfo.getId());
        searchMasterMsg.setUserId(user.getId());
        searchMasterMsgService.publish(searchMasterMsg);
        return AjaxResult.me().success();
    }

    /**
     * 查找待审核
     * @param searchMasterMsgQuery
     * @param request
     * @return
     */
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery,HttpServletRequest request)
    {
        // 查找到userid
//        Logininfo logininfo = LoginContext.getLogininfo(request);
//        List<String> empl = employeeMapper.findByLogininfoId(logininfo.getId());
        searchMasterMsgQuery.setState(0);
        return searchMasterMsgService.queryByPage(searchMasterMsgQuery);
    }

    /**
     * 查找待处理
     * @param searchMasterMsgQuery
     * @param request
     * @return
     */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery,HttpServletRequest request)
    {
        // 查找到userid
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.selectByLogininfoId(logininfo.getId());
        if (employee.getShopId() != null) {
            searchMasterMsgQuery.setShopId(employee.getShopId());
        }
        searchMasterMsgQuery.setState(1);
        return searchMasterMsgService.findByParam(searchMasterMsgQuery);
    }

    /**
     * 拒单
     * @param id
     * @return
     */
    @GetMapping("/reject/{id}")
    public AjaxResult reject(@PathVariable("id") Long id) {
        SearchMasterMsg searchMasterMsg = searchMasterMsgService.findById(id);
        searchMasterMsgService.reject(searchMasterMsg);
        return AjaxResult.me().success();
    }

    /**
     * 查找寻主池
     * @param searchMasterMsgQuery
     * @param request
     * @return
     */
    @PostMapping("/pool")
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery,HttpServletRequest request)
    {
        searchMasterMsgQuery.setShopId(null);
        searchMasterMsgQuery.setState(1);
        PageList<SearchMasterMsg> searchMasterMsgs = searchMasterMsgService.findByParam(searchMasterMsgQuery);
        return searchMasterMsgs;
    }

    /**
     * 接单
     * @param acceptDto
     * @param request
     * @return
     */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody AcceptDto acceptDto, HttpServletRequest request)
    {
        // 查找到userid
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.selectByLogininfoId(logininfo.getId());
        Long shopId = employee.getShopId();
        searchMasterMsgService.accept(acceptDto,shopId);
        return AjaxResult.me().success();
    }

    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery, HttpServletRequest request)
    {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.selectByLogininfoId(logininfo.getId());
        Long shopId = employee.getShopId();
        if (shopId == null) {
            searchMasterMsgQuery.setShopId(shopId);
        }
        return searchMasterMsgService.finish(searchMasterMsgQuery);
    }




}
